package com.moonshineproject.plugin.filesearch.command
{
	import __AS3__.vec.Vector;
	import com.moonshineproject.vo.Settings;
	
	import com.moonshineproject.plugin.console.MarkupTextLineModel;
	import com.moonshineproject.text.TextLineModel;
	import com.moonshineproject.utils.HtmlFormatter;
	
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.events.Event;
	import flash.events.NativeProcessExitEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.utils.IDataInput;
	
	public class FileSearch extends FileSearchBase
	{
		private var result:String = "";
		private var dir:File;
		
		override public function search(s:String, dir:File):void
		{
			var grepPath:File;

			if (Settings.os == "win")
				grepPath = (File.applicationDirectory).resolvePath("assets/tools/grep/grep.exe");
			else
				grepPath = new File("/usr/bin/grep");

			this.dir = dir;
			
			customInfo = new NativeProcessStartupInfo();
			customInfo.executable = grepPath;
			
			var args:Vector.<String> = new Vector.<String>();
			 
			args.push("-n");
			args.push("-r");
			args.push(s);
			args.push(".");
			
			customInfo.arguments = args;
			customInfo.workingDirectory = dir;
			
			customProcess = new NativeProcess();
			customProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, stdErr);
			customProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, stdOut);
			customProcess.addEventListener(NativeProcessExitEvent.EXIT, shellExit);
			customProcess.start(customInfo);
		}
		
		protected function stdOut(event:ProgressEvent):void
		{
			var output:IDataInput = customProcess.standardOutput;
			var data:String = output.readUTFBytes(output.bytesAvailable);
			
			result += data;
		}
		
		protected function stdErr(event:ProgressEvent):void
		{
			
		}
		
		protected function shellExit(event:NativeProcessExitEvent):void
		{
			files = new Vector.<File>();
			parseFiles();
			
			dispatchEvent( new Event(Event.COMPLETE) );
		}
		
		private function parseFiles():void
		{
			var lines:Vector.<TextLineModel> = Vector.<TextLineModel>([]);
			
			var rows:Array = result.split("\n");
			for each (var row:String in rows)
			{
				if (!row) break; 
				//./src/com/moonshineproject/plugin/actionscript/flashproject/MacFlashIDELaunchCommand.as:59:			var file:File = new File(dir.nativePath + File.separator + path.name.split('.')[0] + "_launcher.jsfl");
				var parts:Array = row.split(":");
				var name:String = parts.splice(0, 1)[0].toString().substr(2);
				var line:String = parts.splice(0, 1)[0];
				var content:String = HtmlFormatter.sprintf("%x", parts.join(":"));
				content = content.replace(/\t/g, "");
				
				var text:String = name + "<weak>:</weak>" + line + "\t\t\t\t\t<weak>" + content + "</weak>";
				var lineModel:TextLineModel = new MarkupTextLineModel(text);
				lines.push(lineModel);
			}
			
			lines.push(
				new TextLineModel(lines.length + " matches found")
			);
			
			outputMsg(lines);
		}
		
	}
}
